class ExcelDataHandler {
  constructor(dataInfos) {

    let errorList = [];
    let schoolMap = {}, userMap = {}, sqMap = {};
    function handleNullScore(item, key) {
      dataInfos[key]["scoreCols"].forEach(col => {
        try {
          if (typeof item[col] == "undefined") {
            item["_null_" + col] = 1;
            item[col] = 0;
            return;
          }
          if (typeof item[col] == "string" && (item[col].toLowerCase() == "null" || item[col] == "")) {
            item[col] = 0;
            item["_null_" + col] = 1;
          }
          item[col] = toNumber(item[col]);
          return null;
        } catch (e) {
          console.error("处理异常分数值时，出现异常", e);
          return "处理异常分数值时，出现异常,data:" + key + ", col" + col;
        }

      });
    }

    function handleRegion(item, schoolMap) {
      if (!item.SchoolName) {
        return false;
      }
      item.SchoolName = item.SchoolName.trim();
      let school = schoolMap[item.SchoolName];
      if (school) {
        item.SchoolRegion = school.SchoolRegion;        
        item.School_Cn = school.School_Cn;
        item.School_En = school.School_En;
        item.SchoolNumber = school["SchoolNumber"];
        // item.Rookie = school.Rookie;
        // item["_school"] = school;
      } else {
        return false;
      }
      return true;
    }

    function reportError(name, pos, error, type = "error") {
      errorList.push({ name, pos, error, type });
    }

    function handleData(fullData) {
      errorList = [];

      handleSchool(fullData);
      handleStudent(fullData);
      for(let key in dataInfos){
        if (key == "school" || key=="student"){
          continue;
        }
        (fullData[key] || []).forEach((item,idx) => {
          let error = handleNullScore(item, key);
          if (error) {
            reportError(key, idx + 1, error);
            return;
          }
        });
      }
      return errorList;
    }


    function handleSchool(fullData) {
      schoolMap = {};
      addPreZero(fullData["school"]);
      (fullData["school"] || []).forEach(item => {
        if (!item.School_En) { return; }
        schoolMap[item.School_En.trim()] = item;
      });
    }

    function handleStudent(fullData) {
      userMap = {};
      addPreZero(fullData["student"]);
      let newList = [];
      (fullData["student"] || []).forEach((item, idx) => {
        if (!item || (!item.UserID && !item.SchoolName)) {
          return;
        }
        let error = handleNullScore(item, "student");
        if (error) {
          reportError("Student", idx + 1, error);
          return;
        }
        if (!handleRegion(item, schoolMap)) {
          reportError("Student", idx + 1, "未能匹配到学校信息, name:" + item.SchoolName);
        }
        // item["Name"] = item["FirstName"] + " " + item["LastName"];
        // item["_id"] = item.StudentNumber;
        userMap[item.StudentNumber] = item.UserID;

     

        newList.push(item);
      });
      fullData["student"] = newList;
    }

    this.getData = function (excelFile, options) {
      let wb = XLSX.read(excelFile, { type: 'binary' });
      let fullData = { options };
      for (let name in dataInfos) {
        let item = dataInfos[name];
        let sheetName = item.sheetName;
        let excelData = XLSX.utils.sheet_to_json(wb.Sheets[sheetName]);

        fullData[name] = excelData;
      }
      let errorList = handleData(fullData);
      return [fullData, errorList];
    };

  }
}
